package com.example.online_order.service.impl;

import com.example.online_order.Eception.BussinessException;
import com.example.online_order.Eception.ExceptionType;
import com.example.online_order.mapper.OrdersMapper;
import com.example.online_order.pojo.Orders;
import com.example.online_order.pojo.PageItem;
import com.example.online_order.service.OrdersService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(readOnly = true)
public class OrdersServiceImpl implements OrdersService {


    @Autowired
    OrdersMapper mapper;

    @Override
    public Orders findById(Integer id) {
        Orders orderDb = mapper.findById(id);
        if (orderDb != null)
            return orderDb;
        else{
            throw new BussinessException(ExceptionType.bus_id_not_found,"order not found");
        }

    }

    @Override
    public Orders findByOrderNum(String orderNum) {
        Orders orderDb = mapper.findByOrderNum(orderNum);
        if (orderDb != null)
            return orderDb;
        else{
            throw new BussinessException(ExceptionType.bus_orderNum_not_found,"order not found");
        }
    }

    @Override
    public List<Orders> findLikeUserNameAndProduct(String userName, String goodsName) {
        List<Orders> orderDbList = mapper.findLikeUserNameAndProduct(userName, goodsName);
        if (orderDbList != null)
            return orderDbList;
        else{
            throw new BussinessException(ExceptionType.bus_not_found,"order not found");
        }
    }

    @Override
    public List<Orders> findAll() {
        List<Orders> orderDbList = mapper.findAll();
        if (orderDbList!= null)
            return orderDbList;
        else{
            throw new BussinessException(ExceptionType.bus_not_found,"order not found");
        }

    }


    @Transactional
    @Override
    public Integer addOrder(Orders orders) {
        Integer res = mapper.addOrder(orders);
        if (res!= 1){
            throw new BussinessException(ExceptionType.bus_add_fail,"add order fail");
        }else{
            return res;
        }
    }


    @Transactional
    @Override
    public Integer deleteById(Integer id) {
        Orders orserDb = mapper.findById(id);
        if (orserDb == null){
            throw new BussinessException(ExceptionType.bus_id_not_found,"order not found");
        }else {
            Integer res = mapper.deleteById(id);
            if (res!= 1){
                throw new BussinessException(ExceptionType.bus_delete_fail,"delete order fail");
            } else {
                return res;
            }
        }

    }

    @Transactional
    @Override
    public boolean deleteByids(int[] ids) {
        int ret = mapper.deleteByids(ids);

        if (ret != ids.length) {
            return false;
        } else {
            return true;
        }
    }


    @Transactional
    @Override
    public Integer clientDlite(Integer id) {
        System.out.println("id is" + id);
        Orders orderDb = mapper.findById(id);
        System.out.println(orderDb);
        if (orderDb == null){
            throw new BussinessException(ExceptionType.bus_id_not_found,"order not found");
        }else{
            Integer res = mapper.clientDlite(id);
            if (res!= 1){
                throw new BussinessException(ExceptionType.bus_client_order_delete_fail,"client delete order fail");
            } else {
                return res;
            }
        }

    }

    @Override
    public List<Orders> findByUserName(String userName) {
        List<Orders> orderDb = mapper.findByUserName(userName);
        if (orderDb == null){
            throw new BussinessException(ExceptionType.bus_order_find_userId_not_found,"user id not found");
        }else{
            return orderDb;
        }

    }

    @Override
    public PageItem<Orders> queryPage(Integer current, Integer size ,String userName) {
        PageHelper.startPage(current, size);
        List<Orders> ordersList = mapper.findByUserName(userName);
        Long tolal =((Page<Orders>) ordersList).getTotal();
        return new PageItem<Orders>(tolal, ordersList);


    }

    @Override
    public PageItem<Orders> queryPageAll(Integer current, Integer size) {
        PageHelper.startPage(current, size);
        List<Orders> ordersList = mapper.findAll();
        Long tolal =((Page<Orders>) ordersList).getTotal();
        return new PageItem<Orders>(tolal, ordersList);
    }

    @Override
    public PageItem<Orders> queryConditionPage(Orders order, Integer current, Integer size) {
        return null;
    }


    @Transactional
    @Override
    public Integer clientUpdate(Integer orderid)  {
        Orders orderDb = mapper.findById(orderid);

        if(orderDb ==null)
        {
            throw new BussinessException(ExceptionType.bus_id_not_found,"order not found");
        }else {

            Integer res = mapper.clientUpdate(orderDb);
            if (res!= 1){
                throw new BussinessException(ExceptionType.bus_client_order_update_fail,"client update order fail");
            } else {
                return res;
            }
        }
    }
}
